<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{margin: 0;padding: 0}
        body{
            height: 2000px;
        }
        .nav{
            height: 50px;
            background-color: salmon;
            padding: 0 10%;
            transition: all 1s;
        }
        .nav ul{
            list-style: none;
            overflow: hidden;
        }
        .nav li{
            float: left;
            margin-right: 30px;
        }.nav a{
            line-height: 100px;
        }
        .fixed{
            position: fixed;
            width: 100%;
            background: skyblue;
        }
        .fixed a{
            color: white !important; /* 强制使用 */
        }
    </style>
</head>
<body>
    <div class="nav" id="nav">
        <ul>
            <li>HOME</li>
            <li>ABOUT</li>
            <li>HOBBY</li>
            <li>CONTANCT</li>
        </ul>
    </div>
    <script>
    window.onload = function(){
        var nav =document.getElementById("nav");
        document.onscroll =function(){
            var sTop = document.documentElement.scrollTop;
            console.log(sTop);
            if(sTop>0){
                nav.className = "nav fixed";
            }else {
                nav.className = "nav" ;
            }
        }
    }
    </script>
</body>
</html>